<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
exit;

// Set content type
header('Content-type: text/xml');

// Check input
if (!isset($_GET['success']) || !is_numeric( $_GET[ 'success' ] ) ) {
	echo '<response rewarded="0" message="Invalid input." />';
	die();
}
elseif( $_GET['success'] == 1 && ( !isset($_GET['userid']) || !is_numeric( $_GET[ 'userid' ] ) ) ) {
	echo '<response rewarded="0" message="No reward given, because no valid user id was supplied." />';
	die();
}

include_once( 'old_database_connection.php' );
include( 'utils.php' );

// Get data
$success = $_GET['success'];
$user_id = $_GET['userid'];

$query = "SELECT timestamp " .
		"FROM vote_reward " .
		"WHERE user_id = $user_id " .
		"ORDER BY timestamp DESC " .
		"LIMIT 1";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$check = $row[ 'timestamp' ];

$timestamp = time();
// Has this user already claimed a reward today?
if ( $timestamp < $check + 3600 ) {
	echo '<response rewarded="0" message="You have already received a vote reward today." />';
	die();
}

$query = "SELECT count(*) " .
		"FROM users " .
		"WHERE user_id = $user_id ";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);

if( $row[ 'count(*)' ] == 0 ) {
	echo '<response rewarded="0" message="Unknown user id supplied, no reward given." />';
	die();
}

// Credit reward
if ($success == 1) {
	$turn = getCurrentTurn();
	$date = getCurrentDate();
	$query = "INSERT INTO vote_reward " .
			"( user_id, timestamp, turn, site, reward ) " .
			"VALUES( $user_id, $timestamp, $turn, 'PBBG Warp', 4 )";
	mysql_query( $query ) or die( "Query failed : " . mysql_error() );

	$query = "SELECT premium " .
			"FROM users " .
			"WHERE user_id = $user_id ";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
	if( $row[ 'premium' ] == -1 ) {
		$query = "UPDATE users " .
				"SET premium = 4 " .
				"WHERE user_id = $user_id";
		mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	}
	else {
		$query = "UPDATE users " .
				"SET premium = premium + 4 " .
				"WHERE user_id = $user_id";
		mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	}
	
	$query = "SELECT ruler_id " .
			"FROM ruler " .
			"WHERE user_id = $user_id ";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
	if( isset( $row[ 'ruler_id' ] ) ) {	
		$ruler_id = $row[ 'ruler_id' ];
		$subject = "Voting reward received";
		$text = "You have received 4 premium turn for voting on Ages of Strife on PBBG Warp.\n" .
				"\n" .
				"Thank you for your vote";
		
		$query = "INSERT INTO news " .
				"( ruler_id, type, news, date, subject, seen, turn ) " .
				"VALUES ( $ruler_id, 2, '" . addslashes( $text ) . "', '$date', '" . addslashes( $subject ) . "', 0, $turn )";
		mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	}
	
	echo '<response rewarded="1" message="You have been rewarded 4 premium turns for Ages of Strife for your vote!" />';
	die();
}
else {
	echo '<response rewarded="0" message="Your vote failed, so no reward was given." />';
	die();
}

?>
